package com.qf.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.DqGroundsMapper;
import com.qf.dto.DqGroundsDTO;
import com.qf.dto.PageDTO;
import com.qf.pojo.DqGrounds;
import com.qf.pojo.DqGroundsExample;
import com.qf.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

/**
 * //                       .::::.
 * //                     .::::::::.
 * //                    :::::::::::  come on
 * //                 ..:::::::::::'
 * //              '::::::::::::'
 * //                .::::::::::
 * //           '::::::::::::::..
 * //                ..::::::::::::.
 * //              ``::::::::::::::::
 * //               ::::``:::::::::'        .:::.
 * //              ::::'   ':::::'       .::::::::.
 * //            .::::'      ::::     .:::::::'::::.
 * //           .:::'       :::::  .:::::::::' ':::::.
 * //          .::'        :::::.:::::::::'      ':::::.
 * //         .::'         ::::::::::::::'         ``::::.
 * //     ...:::           ::::::::::::'              ``::.
 * //    ```` ':.          ':::::::::'                  ::::..
 * //                       '.:::::'                    ':'````..
 * //
 *
 * @author:小川
 * @date: 2018/11/23 17:42
 */
@Service
public class DqGroundsServiceImpl implements DqGroundsService {
    @Autowired
    private DqGroundsMapper dqGroundsMapper;

    @Override
    public int insert(DqGroundsDTO dto) throws ParseException {
        DqGrounds grounds=new DqGrounds();
        BeanUtils.copyProperties(dto,grounds);
        grounds.setCreatedAt(DateUtils.stringToDate(dto.getCreatedAt()));
        grounds.setUpdatedAt(DateUtils.stringToDate(dto.getUpdatedAt()));
        int i = dqGroundsMapper.insert(grounds);
        return grounds.getId();
    }

    @Override
    public PageDTO findAll(int page, int rows) {
        PageDTO pageDTO=new PageDTO();
        PageHelper.startPage(page, rows);
        DqGroundsExample example=new DqGroundsExample();
        List<DqGrounds> dqGrounds = dqGroundsMapper.selectByExample(example);
        PageInfo<DqGrounds> info=new PageInfo<>(dqGrounds);
        pageDTO.setTotal(info.getTotal());
        pageDTO.setRows(info.getList());
        return pageDTO;
    }

    @Override
    public int update(DqGroundsDTO dto) throws ParseException {
        DqGrounds dqGrounds=new DqGrounds();
        BeanUtils.copyProperties(dto,dqGrounds);
        dqGrounds.setUpdatedAt(DateUtils.stringToDate(dto.getUpdatedAt()));
        int i = dqGroundsMapper.updateByPrimaryKeySelective(dqGrounds);
        return i;
    }

    @Override
    public void delete(int id) {
        dqGroundsMapper.deleteByPrimaryKey(id);
    }
}
